//
//  GrowthRecordTableViewCell.m
//  LoveDraw
//
//  Created by 梁晨 on 2017/6/26.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "GrowthRecordTableViewCell.h"
#import "GrowthRecordCollectionViewCell.h"


@implementation GrowthRecordTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(UICollectionView *)imageCollectionV
{
    if (!_imageCollectionV) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 6;
        layout.minimumInteritemSpacing = 6;
        layout.itemSize = CGSizeMake(80, 80);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

        _imageCollectionV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _imageCollectionV.backgroundColor = [UIColor whiteColor];
        _imageCollectionV.delegate = self;
        _imageCollectionV.dataSource = self;
        [_imageCollectionV registerNib:[UINib nibWithNibName:@"GrowthRecordCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"GrowthRecordCollectionViewCell"];
    }
    return _imageCollectionV;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //初始化模块
        self.YeartimeL = [[UILabel alloc]init];
        self.dateL = [[UILabel alloc]init];
        self.TopLine = [[UIView alloc]init];
        self.bottomline = [[UIView alloc]init];
        self.CycleV = [[UIView alloc]init];
        self.titleL = [[UILabel alloc]init];
        self.detailL = [[UILabel alloc]init];


//        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//        layout.minimumLineSpacing = 10;
//        layout.minimumInteritemSpacing = 10;
//        layout.itemSize = CGSizeMake(80, 80);
//        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//        
//        self.imageCollectionV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
//        self.imageCollectionV.backgroundColor = [UIColor whiteColor];
//        self.imageCollectionV.delegate = self;
//        self.imageCollectionV.dataSource = self;
//        [self.imageCollectionV registerNib:[UINib nibWithNibName:@"GrowthRecordCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"GrowthRecordCollectionViewCell"];

        //添加到父视图上
        [self.contentView addSubview:self.YeartimeL];
        [self.contentView addSubview:self.dateL];
        [self.contentView addSubview:self.titleL];
        [self.contentView addSubview:self.detailL];
        [self.contentView addSubview:self.imageCollectionV];
        [self.contentView addSubview:self.TopLine];
        [self.contentView addSubview:self.self.bottomline];
        [self.contentView addSubview:self.CycleV];
        
        //给属性赋值阶段
        self.YeartimeL.font = [UIFont systemFontOfSize:14];
        self.YeartimeL.textAlignment = 1;
        self.YeartimeL.textColor = UIColors(0x333333);
        
        self.dateL.font = [UIFont systemFontOfSize:13];
        self.dateL.textAlignment = 1;
        self.dateL.textColor = UIColors(0x666666);
        
        self.TopLine.backgroundColor = UIColors(0xEFEFEF);
        self.bottomline.backgroundColor = UIColors(0xEFEFEF);
        self.detailL.numberOfLines = 0;
        
        self.CycleV.layer.masksToBounds = YES;
        self.CycleV.layer.cornerRadius = 8;
        self.CycleV.layer.borderColor = PKCOLOR(255, 43, 117).CGColor;
        self.CycleV.layer.borderWidth = 1;
        self.CycleV.backgroundColor = [UIColor whiteColor];

        self.DeleteBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
        [self.DeleteBtn setTitle:@"删除" forState:(UIControlStateNormal)];
        [self.DeleteBtn addTarget:self action:@selector(DeleteAct) forControlEvents:(UIControlEventTouchUpInside)];
        [self.DeleteBtn setTitleColor:UIColors(MainColor) forState:(UIControlStateNormal)];
        [self.contentView addSubview:self.DeleteBtn];

    }
    return self;
}

-(void)setListArr:(NSMutableArray *)listArr
{
    _listArr = listArr;
//    CGFloat picContainerH = [self.picContainerView setupPicUrlArray:_listArr];
//    self.picContainerView.picOriArray = _listArr;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.YeartimeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(10);
        make.top.equalTo(self.contentView.mas_top).offset(20);
        make.width.mas_equalTo(40);
    }];
    
    [self.dateL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.YeartimeL.mas_centerX);
        make.top.equalTo(self.YeartimeL.mas_bottom);
    }];
    
    [self.TopLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.YeartimeL.mas_right).offset(10);
        make.width.mas_equalTo(1);
        make.bottom.equalTo(self.CycleV.mas_top);
    }];

    [self.bottomline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.CycleV.mas_bottom);
        make.left.equalTo(self.YeartimeL.mas_right).offset(10);
        make.width.mas_equalTo(1);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    
    [self.CycleV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.YeartimeL.mas_bottom).offset(-5);
        make.centerX.equalTo(self.TopLine.mas_centerX);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];

    if (self.listArr.count == 0 && self.detailL.text.length == 0)
    {
        [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.TopLine.mas_right).offset(8);
            make.centerY.equalTo(self.CycleV.mas_centerY);
        }];
    }else
    {
        [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.TopLine.mas_right).offset(8);
            make.top.equalTo(self.contentView.mas_top).offset(15);
        }];
    }

    [self.DeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-20);
        make.centerY.equalTo(self.titleL.mas_centerY);
    }];
    
    [self.detailL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.TopLine.mas_right).offset(8);
        make.top.equalTo(self.titleL.mas_bottom).offset(5);
        make.right.equalTo(self.contentView.mas_right).offset(-20);
    }];
    
    [self.imageCollectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.TopLine.mas_right).offset(8);
        make.width.mas_equalTo((80+6)*3);
        make.top.equalTo(self.detailL.mas_bottom).offset(10);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GrowthRecordCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GrowthRecordCollectionViewCell" forIndexPath:indexPath];

    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,self.listArr[indexPath.row]]]];


//    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,self.listArr[indexPath.row]]] placeholderImage:nil];

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.imageCount = self.listArr.count;
    browser.currentImageIndex = indexPath.row;
    browser.sourceImagesContainerView = self.imageCollectionV;
    browser.delegate = self;
    [browser show];
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSURL *url = [NSURL new];
    NSMutableArray *thumbUrlArr = [NSMutableArray array];
    for (NSString *url in self.listArr)
    {
     [thumbUrlArr addObject:[url stringByReplacingOccurrencesOfString:@"/thumb" withString:@""]];
    }
    if (index < thumbUrlArr.count) {
        url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,thumbUrlArr]];
    }
    return url;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    GrowthRecordCollectionViewCell *Cell = (GrowthRecordCollectionViewCell *)[self.imageCollectionV cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    UIImageView *imageView = Cell.imageV;
    return imageView.image;
}


-(void)DeleteAct
{
    
    [self.Delegate DeleteCell:self.DeleteBtn];
}



@end
